package com.polaris.live.dialog

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.view.View
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils
import com.polaris.live.R
import com.polaris.live.adapter.level.LevelUpBannerAdapter
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.DialogLevelUpgradeBinding
import com.polaris.live.im.sfs.bean.chat.UserRichCharmLevelUpgradeNoticeMessage
import com.polaris.live.translation.R.string
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.zhpan.indicator.enums.IndicatorSlideMode

/**
 * LevelUpDialog
 *
 * @author Created by 半仙 on 2024/1/17/017 9:54
 */
class LevelUpgradeDialog(
    context: Context,
    private val data: UserRichCharmLevelUpgradeNoticeMessage,
) : CenterPopupView(context) {

    private lateinit var mBinding: DialogLevelUpgradeBinding

    private val mAdapter by lazy { LevelUpBannerAdapter() }
    override fun getImplLayoutId() = R.layout.dialog_level_upgrade

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogLevelUpgradeBinding.bind(popupImplView)
        mBinding.levelBanner.apply {
            registerLifecycleObserver(lifecycle)
            setAdapter(mAdapter)
            setIndicatorSlideMode(IndicatorSlideMode.NORMAL)
            setIndicatorVisibility(View.GONE)
            setAutoPlay(false)
            setCanLoop(false)
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    val isFirstItem = position == 0
                    val isLastItem = position == mAdapter.itemCount - 1

                    mBinding.ivLeft.visibility = if (isFirstItem) View.GONE else View.VISIBLE
                    mBinding.ivRight.visibility = if (isLastItem) View.GONE else View.VISIBLE
                }
            })
        }
        mBinding.levelBanner.create(data.rewards)
        mBinding.ivLeft.setOnClickListener {
            val getNow = mBinding.levelBanner.currentItem
            if (getNow - 1 >= 0) {
                mBinding.levelBanner.setCurrentItem(getNow - 1, true)
            }
        }
        mBinding.ivRight.setOnClickListener {
            val getNow = mBinding.levelBanner.currentItem
            if (getNow + 1 <= mAdapter.itemCount) {
                mBinding.levelBanner.setCurrentItem(getNow + 1, true)
            }
        }
        if (mAdapter.itemCount <= 1) {
            mBinding.ivLeft.visibility = View.GONE
            mBinding.ivRight.visibility = View.GONE
        }

        mAdapter.setOnRouterClick { position ->
            val item = data.rewards?.getOrNull(position) ?: return@setOnRouterClick
            if (item.rewardType == LevelUpBannerAdapter.BACK) {
                Router.builder(RoutePath.BACKPACK_ACTIVITY).navigation(context)
            } else if (item.rewardType == LevelUpBannerAdapter.HONOR) {
                Router.builder(RoutePath.HONOR_ACTIVITY).navigation(context)
            }
        }

        if (data.type == RICH) {
            mBinding.tvLevel.text = StringUtils.getString(string.your_richs_level_up, data.level)
            mBinding.ivLeft.setBackgroundResource(R.drawable.rich_img_jiantouzuo)
            mBinding.ivRight.setBackgroundResource(R.drawable.rich_img_jiantouyou)
            mBinding.dialogBack.setImageResource(R.drawable.rich_icon_tanchuang)
        } else {
            mBinding.tvLevel.text = StringUtils.getString(string.your_charms_level_up, data.level)
            mBinding.ivLeft.setBackgroundResource(R.drawable.charm_img_jiantouzuo)
            mBinding.ivRight.setBackgroundResource(R.drawable.charm_img_jiantouyou)
            mBinding.dialogBack.setImageResource(R.drawable.charm_icon_tanchuang)
        }
        mBinding.icClose.setOnSingleClickListener {
            dismiss()
        }
        animationStart()
        animationStartRight()
    }

    private fun animationStart() {
        val translateAnimationLeft =
            ObjectAnimator.ofFloat(mBinding.ivLeft, "translationX", 0f, -40f)
        translateAnimationLeft.duration = 500

        val translateAnimationRight =
            ObjectAnimator.ofFloat(mBinding.ivLeft, "translationX", -40f, 0f)
        translateAnimationRight.duration = 500

        val animatorSet = AnimatorSet()
        animatorSet.playSequentially(translateAnimationLeft, translateAnimationRight)

        animatorSet.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                if (!isDismiss) {
                    animatorSet.start()
                }
            }
        })

        animatorSet.start()
    }

    private fun animationStartRight() {
        val translateAnimationLeft =
            ObjectAnimator.ofFloat(mBinding.ivRight, "translationX", 40f, 0f)
        translateAnimationLeft.duration = 500

        val translateAnimationRight =
            ObjectAnimator.ofFloat(mBinding.ivRight, "translationX", 0f, 40f)
        translateAnimationRight.duration = 500

        val animatorSet = AnimatorSet()
        animatorSet.playSequentially(translateAnimationRight, translateAnimationLeft)

        animatorSet.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                if (!isDismiss) {
                    animatorSet.start()
                }
            }
        })

        animatorSet.start()
    }

    override fun getMaxWidth(): Int {
        if (popupInfo == null) return 0
        return if (popupInfo.maxWidth == 0) (XPopupUtils.getAppWidth(context) * 1f).toInt() else popupInfo.maxWidth
    }

    companion object {
        const val CHARM = 1
        const val RICH = 0
    }
}